package com.bus.core.manager.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bus.common.hibernate4.Updater;
import com.bus.core.dao.BusSiteDao;
import com.bus.core.entity.BusSite;
import com.bus.core.entity.BusUser;
import com.bus.core.manager.BusSiteMng;
import com.bus.core.manager.BusUserMng;
import com.bus.core.manager.BusUserSiteMng;

@Service("busSiteMng")
@Transactional
public class BusSiteMngImpl implements BusSiteMng {
	private static final Logger log = LoggerFactory
			.getLogger(BusSiteMngImpl.class);

	@Transactional(readOnly = true)
	public List<BusSite> getList() {
		return dao.getList(false);
	}
	
	@Transactional(readOnly = true)
	public List<BusSite> getListFromCache() {
		return dao.getList(true);
	}
	
	@Transactional(readOnly = true)
	public boolean hasRepeatByProperty(String property){
		return (getList().size()-dao.getCountByProperty(property))>0?true:false;
	}
	
	@Transactional(readOnly = true)
	public BusSite findByDomain(String domain) {
		return dao.findByDomain(domain);
	}

	@Transactional(readOnly = true)
	public BusSite findById(Integer id) {
		BusSite entity = dao.findById(id);
		return entity;
	}
	
	@Override
	public BusSite update(BusSite site) {
		Updater<BusSite> updater = new Updater<BusSite>(site);
		site = dao.updateByUpdater(updater);
		return site;
	}
	

	public void updateTplSolution(Integer siteId, String solution,String mobileSol) {
		BusSite site = findById(siteId);
		if(StringUtils.isNotBlank(solution)){
			site.setTplSolution(solution);
		}
		if(StringUtils.isNotBlank(mobileSol)){
			site.setTplMobileSolution(mobileSol);
		}
	}
	
	public void updateAttr(Integer siteId,Map<String,String>attr){
		BusSite site = findById(siteId);
		site.getAttr().putAll(attr);
	}
	
	public void updateAttr(Integer siteId,Map<String,String>...attrs){
		BusSite site = findById(siteId);
		for(Map<String,String>m:attrs){
			site.getAttr().putAll(m);
		}
	}

	public BusSite deleteById(Integer id) {
		// 删除用户、站点关联
		busUserSiteMng.deleteBySiteId(id);
		BusSite bean = dao.findById(id);
		dao.deleteById(id);
		log.info("delete site "+id);
		// 删除模板
		/*
		try {
			busResourceMng.delTplAndRes(bean);
		} catch (IOException e) {
			log.error("delete Template and Resource fail!", e);
		}
		*/
		return bean;
	}

	public BusSite[] deleteByIds(Integer[] ids) {
		BusSite[] beans = new BusSite[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private BusUserMng busUserMng;
	private BusUserSiteMng busUserSiteMng;
	private BusSiteDao dao;

	@Autowired
	public void setBusUserMng(BusUserMng busUserMng) {
		this.busUserMng = busUserMng;
	}

	@Autowired
	public void setBusUserSiteMng(BusUserSiteMng busUserSiteMng) {
		this.busUserSiteMng = busUserSiteMng;
	}

	@Autowired
	public void setDao(BusSiteDao dao) {
		this.dao = dao;
	}

	@Override
	public BusSite save(BusSite currSite, BusUser currUser, BusSite bean,
			Integer uploadFtpId, Integer syncPageFtpId) throws IOException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public BusSite update(BusSite bean, Integer uploadFtpId,
			Integer syncPageFtpId, Integer ossId) {
		// TODO Auto-generated method stub
		return null;
	}


}